package com.zzptc.a114wh_workapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import com.zzptc.a114wh_workapplication.entity.Book;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    int[] pics={R.mipmap.ic_launcher,R.mipmap.ic_launcher};
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg!=null){
                Bitmap b = (Bitmap) msg.obj;
            }

        }
    };

    ImageView imageView;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "run: "+Thread.currentThread().getName());
                initData();//准备数据
            }
        }).start();
        Log.d(TAG, "onCreate: "+Thread.currentThread().getName());
        //Book book1 = new Book(pics[0], 1,"挪威森林", 29.8f);
        //Book book2 = new Book(pics[1], 1,"贝加尔湖畔", 29.8f);


    }
   public void initData(){
       try {
           URL url=new URL("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1604457769&di=628ba0f91fc6242e5f1674d90935992b&src=http://a1.att.hudong.com/05/00/01300000194285122188000535877.jpg");
           HttpURLConnection caaa = (HttpURLConnection) url.openConnection();
           //设置访问方式get  post
           caaa.setRequestMethod("GET");
           //判断服务器的返回结果
           int innn = caaa.getResponseCode();
           if (innn==200) {
               InputStream inputStream = caaa.getInputStream();
               Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
               //imageView.setImageBitmap(bitmap);
               //往主线程发消息      放数据
               Message message = handler.obtainMessage();
               message.obj=bitmap;
               handler.sendMessage(message);

           }


       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }


    public void init(){
        imageView=findViewById(R.id.Lv);
    }
}
